﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Channels.Models;
using XWT.Data;
using System.Data.SqlClient;
using System.Data;

namespace Channels {
    public class ChannelsLogic {
        public static List<Channel> GetChannels() {
            Database db = Database.Create();
            IDataReader rdr = db.ExecuteReader("SELECT * FROM CHANNELS");

            List<Channel> channels = new List<Channel>();

            while (rdr.Read()) {
                Channel c = new Channel() {
                    Id = Convert.ToInt32(rdr["CHA_ID"]),
                    Title = rdr["CHA_TITLE"].ToString()
                };

                channels.Add(c);
            }
            rdr.Close();
            return channels;
        }

        public static List<Channel> GetChannels(string username) {
            Database db = Database.Create();
            IDataReader rdr = db.ExecuteReader(string.Format("SELECT * FROM USER_CHANNELS where USR_EMAIL='{0}'", username));

            List<Channel> channels = new List<Channel>();

            while (rdr.Read()) {
                Channel c = new Channel() {
                    Id = Convert.ToInt32(rdr["CHA_ID"]),
                    Title = rdr["CHA_TITLE"].ToString()
                };

                channels.Add(c);
            }
            rdr.Close();
            return channels;
        }

        public static ChannelItem GetChannelItem(int channelItemId) {
            Database db = Database.Create();
            IDataReader rdr = db.ExecuteReader("SELECT * FROM CHANNEL_ITEMS WHERE CHA_ITM_ID = " + channelItemId.ToString());

            List<ChannelItem> channelItems = new List<ChannelItem>();

            while (rdr.Read()) {
                ChannelItem c = new ChannelItem() {
                    Id = Convert.ToInt32(rdr["CHA_ITM_ID"]),
                    ChannelId = Convert.ToInt32(rdr["CHA_ID"]),
                    Title = rdr["CHA_ITM_TITLE"].ToString(),
                    ImageUrl = WrapImage(rdr["CHA_ITM_IMAGEURL"].ToString()),//.Length > 10 ? rdr["CHA_ITM_DESC"].ToString().Substring(0, 30) : rdr["CHA_ITM_DESC"].ToString()
                    Desc = rdr["CHA_ITM_DESC"].ToString().Length > 250 ? string.Format("{0}...", rdr["CHA_ITM_DESC"].ToString().Substring(0, 240)) : rdr["CHA_ITM_DESC"].ToString(),
                    Link = rdr["CHA_ITM_LINK"].ToString()//.Length > 10 ? rdr["CHA_ITM_DESC"].ToString().Substring(0, 30) : rdr["CHA_ITM_DESC"].ToString()
                };

                channelItems.Add(c);
            }
            rdr.Close();
            return channelItems.Count > 0 ? channelItems[0] : null;
        }

        public static List<ChannelItem> GetChannelItems(int channelId) {
            Database db = Database.Create();
            IDataReader rdr = db.ExecuteReader("SELECT * FROM CHANNEL_ITEMS WHERE CHA_ID = " + channelId.ToString());

            List<ChannelItem> channelItems = new List<ChannelItem>();

            while (rdr.Read()) {
                ChannelItem c = new ChannelItem() {
                    Id = Convert.ToInt32(rdr["CHA_ITM_ID"]),
                    ChannelId = Convert.ToInt32(rdr["CHA_ID"]),
                    Title = rdr["CHA_ITM_TITLE"].ToString(),
                    ImageUrl = WrapImage(rdr["CHA_ITM_IMAGEURL"].ToString()),//.Length > 10 ? rdr["CHA_ITM_DESC"].ToString().Substring(0, 30) : rdr["CHA_ITM_DESC"].ToString()
                    Desc = rdr["CHA_ITM_DESC"].ToString().Length > 250 ? string.Format("{0}...", rdr["CHA_ITM_DESC"].ToString().Substring(0, 240)) : rdr["CHA_ITM_DESC"].ToString(),
                    Link = rdr["CHA_ITM_LINK"].ToString()//.Length > 10 ? rdr["CHA_ITM_DESC"].ToString().Substring(0, 30) : rdr["CHA_ITM_DESC"].ToString()
                };

                channelItems.Add(c);
            }
            rdr.Close();
            return channelItems;
        }

       
        public static bool CreateUserChannels(string username, int chaId) {
            try {
                Database db = Database.Create();

                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.AddWithValue("@USR_EMAIL", username);
                cmd.Parameters.AddWithValue("@CHA_ID", chaId);
                cmd.Parameters.AddWithValue("@USR_CHA_PRIORITY", 0);
                cmd.Parameters.AddWithValue("@USR_CHA_ACTIVE", true);

                db.ExecuteNonQuery("USER_CHANNELS_INSERT", cmd);
                return true;

            } catch (Exception ex) {
                return false;
            }
        }

        private static string WrapImage(string imageUrl) {
            if (imageUrl.Length > -1) {
                return string.Format("<div class=\"c-image\">{0}</div>", imageUrl);
            }
            return imageUrl;
        }
        
    }
}